package cn.butcher21.dao.DaoWorkImpl;

import cn.butcher21.dao.DaoUser;
import cn.butcher21.pojo.Group;
import cn.butcher21.utils.DButil;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DaoUserImpl implements DaoUser {
    public static void main(String[] args) throws SQLException {
        DaoUserImpl daoUser = new DaoUserImpl();
        Map<String, Object> groupMsg = daoUser.getGroupMsg("17");
        System.out.println(groupMsg);
    }

    /**
     * 检查用户名和密码是否正确
     *
     * @param username 用户名
     * @param password 密码
     * @return 正确返回小组id，否则返回null
     * @throws SQLException 异常
     */
    @Override
    public String[] check(String username, String password) throws SQLException {
        DButil dButil = new DButil();
        String sql = "SELECT gid,gpower FROM groups WHERE gname = ? AND gpassword = ?";
        PreparedStatement ps = dButil.getPs(sql);
        ps.setString(1, username);
        ps.setString(2, password);
        ResultSet rs = dButil.getRs(ps.executeQuery());
        String[] s = {"", ""};
        if (rs.next()) {
            s[0] = rs.getString(1);
            s[1] = rs.getString(2);
            return s;
        }
        return null;
    }

    /**
     * 获取小组信息
     *
     * @param id 小组编号
     * @return map形式的键值对
     * @throws SQLException 异常
     */
    @Override
    public Map<String, Object> getGroupMsg(String id) throws SQLException {
        Map<String, Object> map = new HashMap<>();
        // 小组成员、小组项目、小组权限、所有小组的id与名字


        DButil dButil = new DButil();

        String sql = "SELECT gpower FROM groups WHERE gid = ?";
        PreparedStatement ps = dButil.getPs(sql);
        ps.setString(1, id);
        ResultSet rs = dButil.getRs(ps.executeQuery());
        if (rs.next()) {
            // 判断是管理还是普通小组
            int power = rs.getInt(1);
            if (power == 1) {
                // 说明是管理，把所有小组的名字与id查出来
                sql = "SELECT gid,gname  FROM groups WHERE gpower = '0'";
                ps = dButil.getPs(sql);
                rs = dButil.getRs(ps.executeQuery());
                List<Map<String, String>> groups = new ArrayList<>();
                while (rs.next()) {
                    Map<String, String> group = new HashMap<>();
                    group.put("id", rs.getString("gid"));
                    group.put("name", rs.getString("gname"));
                    groups.add(group);
                }

                map.put("groups", groups);
            }
            // 获取小组名字，项目，成员
            sql = "SELECT p_id,p_name FROM projects WHERE gid = ? AND  p_status = '0'";
            ps = dButil.getPs(sql);
            ps.setInt(1, Integer.parseInt(id));
            rs = dButil.getRs(ps.executeQuery());
            List<Map<String, String>> projects = new ArrayList<>();
            while (rs.next()) {
                Map<String, String> p = new HashMap<>();
                p.put("id", rs.getString(1));
                p.put("name", rs.getString(2));
                projects.add(p);
            }
            map.put("projects", projects);

            sql = "SELECT username FROM users WHERE gid = ?";
            ps = dButil.getPs(sql);
            ps.setInt(1, Integer.parseInt(id));
            rs = dButil.getRs(ps.executeQuery());
            List<String> members = new ArrayList<>();
            while (rs.next()) {
                members.add(rs.getString(1));
            }
            map.put("members", members);

            map.put("id", id);

            sql = "SELECT gname  FROM groups WHERE gid = ?";
            ps = dButil.getPs(sql);
            ps.setInt(1, Integer.parseInt(id));
            rs = dButil.getRs(ps.executeQuery());
            if (rs.next()) map.put("name", rs.getString(1));

            return map;
        }
        return null;
    }
}
